using Abc.Core;
using Fur;
using Furion.DatabaseAccessor;
using Furion.DependencyInjection;
using Mapster;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Utils;

namespace Abc.Application
{
    [Injection(Proxy = typeof(LogDispatchProxy))]
    public class SecurityServices : ISecurityServices, ITransient
    {
        private readonly IRepository<Security> _securityRepository;
        public SecurityServices(IRepository<Security> securityRepository)
        {
            _securityRepository = securityRepository;
        }
        /// <summary>
        /// 新增一条
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<ResultData<string>> Insert(SecurityDto input)
        {
            var resultdata = new ResultData<string>();
            // 如果不需要返回自增Id，使用 InsertAsync即可
            var newEntity = await _securityRepository.InsertAsync(input.Adapt<Security>());


            resultdata.State = true;
            resultdata.Msg = "成功";
            return resultdata;
        }
        /// <summary>
        /// 更新一条
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<ResultData<string>> Update(SecurityDto input)
        {
            var resultdata = new ResultData<string>();
            var dbentity = await _securityRepository.Entities.SingleAsync(u => u.Id == input.Id);
            input.Adapt(dbentity);
            await _securityRepository.UpdateAsync(dbentity);


            resultdata.State = true;
            resultdata.Msg = "成功";
            return resultdata;
        }
        /// <summary>
        /// 新增或更新一条
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<ResultData<string>> AddOrUpdate(SecurityDto input)
        {
            if (input.Id > 0)
            {
                return await Update(input);
            }
            else
            {
                return await Insert(input);
            }
        }
        /// <summary>
        /// 删除一条
        /// </summary>
        /// <param name="id"></param>
        public async Task<ResultData<string>> Delete(int id)
        {
            var resultdata = new ResultData<string>();
            await _securityRepository.DeleteNowAsync(id);


            resultdata.State = true;
            resultdata.Msg = "成功";
            return resultdata;
        }
        /// <summary>
        /// 查询一条
        /// </summary>
        /// <param name="id"></param>
        public async Task<SecurityDto> Find(int id)
        {
            var dbentity = await _securityRepository.FindAsync(id);
            return dbentity.Adapt<SecurityDto>();
        }
        /// <summary>
        /// 查询所有
        /// </summary>
        /// <returns></returns>
        public async Task<List<SecurityDto>> GetAll()
        {
            var queryable = _securityRepository.AsQueryable();
            var entities = queryable.ProjectToType<SecurityDto>();
            return await entities.ToListAsync();
        }
        ///// <summary>
        ///// 分页查询
        ///// </summary>
        ///// <param name="querydto"></param>
        ///// <returns></returns>
        //[NonTransact]
        //public async Task<Fur.DatabaseAccessor.PagedList<SecurityDto>> GetAllByPage(SecurityQueryDto querydto)
        //{
        //    var pageResult = await _securityRepository.AsQueryable().ProjectToType<SecurityDto>()
        //        .ToPagedListAsync(querydto.Page, querydto.Limit);

        //    foreach (var entity in pageResult.Items)
        //    {
        //        entity.ParentSecurity = GetParentSecurity(pageResult.Items, entity.ParentId);
        //    }

        //    return pageResult;
        //}
        SecurityDto GetParentSecurity(IEnumerable<SecurityDto> securitys, int? id)
        {
            if (id == null) return null;
            var tempsecurity = securitys.FirstOrDefault(o => o.Id == id);
            if (tempsecurity != null)
            {
                return tempsecurity;
            }
            return null;
        }
        string GetParentSecurityName(IEnumerable<SecurityDto> securitys, int? id)
        {
            if (id == null) return "";
            var tempsecurity = securitys.FirstOrDefault(o => o.Id == id);
            if (tempsecurity != null)
            {
                return tempsecurity.Name;
            }
            return "";
        }
        string GetFullName(List<SecurityDto> securitys, int? pid, string fullname)
        {
            if (pid == null) return fullname;
            var tempsecurity = securitys.FirstOrDefault(o => o.Id == pid);
            if (tempsecurity != null)
            {
                fullname = $"{tempsecurity.Name}-{fullname}";
                GetFullName(securitys, tempsecurity.ParentId, fullname);
            }

            return fullname;
        }


        //[NonTransact]
        //public async Task RefreshCache()
        //{
        //    var entities = await _securityRepository.AsQueryable().ToListAsync();

        //    _memoryCache.Set(CacheKey.Security, entities, new MemoryCacheEntryOptions()
        //    {
        //        SlidingExpiration = new System.TimeSpan(1, 1, 1, 1),
        //        //AbsoluteExpiration=DateTimeOffset.Now.AddDays(10)
        //    });

        //}

    }
    public interface ISecurityServices
    {
        /// <summary>
        /// 新增一条
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        Task<ResultData<string>> Insert(SecurityDto input);

        /// <summary>
        /// 更新一条
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        Task<ResultData<string>> Update(SecurityDto input);


        /// <summary>
        /// 新增或更新一条
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        Task<ResultData<string>> AddOrUpdate(SecurityDto input);


        /// <summary>
        /// 删除一条
        /// </summary>
        /// <param name="id"></param>
        Task<ResultData<string>> Delete(int id);

        /// <summary>
        /// 查询一条
        /// </summary>
        /// <param name="id"></param>
        Task<SecurityDto> Find(int id);

        /// <summary>
        /// 查询所有
        /// </summary>
        /// <returns></returns>
        Task<List<SecurityDto>> GetAll();


        ///// <summary>
        ///// 分页查询
        ///// </summary>
        ///// <param name="querydto"></param>
        ///// <returns></returns>
        //[NonTransact]
        //Task<PagedListToLayui<SecurityDto>> GetAllByPageToLayui(SecurityQueryDto querydto);
        ///// <summary>
        ///// 更新多条
        ///// </summary>
        ///// <param name="inputs"></param>
        ///// <returns></returns>
        //Task<ResultData<string>> Update(List<SecurityDto> inputs);
        ///// <summary>
        ///// 新增多条
        ///// </summary>
        ///// <param name="inputs"></param>
        ///// <returns></returns>
        //Task<ResultData<string>> Insert(List<SecurityDto> inputs);
        ///// <summary>
        ///// 新增或更新多条
        ///// </summary>
        ///// <param name="inputs"></param>
        ///// <returns></returns>
        //Task<ResultData<string>> AddOrUpdate(List<SecurityDto> inputs);

        ///// <summary>
        ///// 分页查询
        ///// </summary>
        ///// <param name="querydto"></param>
        ///// <returns></returns>
        //[NonTransact]
        //Task<Fur.DatabaseAccessor.PagedList<SecurityDto>> GetAllByPage(SecurityQueryDto querydto);
    }


}
